#include <stdio.h>
#include <time.h>
#include "/home/haiyang/wendang/Cprogrammer/include/stats_saving.h"

void save_stats_to_file(TrafficStats *stats) {
    time_t now = time(NULL);
    struct tm *tm_info = localtime(&now);
    char filename[20];
    strftime(filename, sizeof(filename), "%Y-%m-%dT%H:00", tm_info);

    FILE *file = fopen(filename, "w");
    if (file == NULL) {
        perror("Failed to open file");
        return;
    }

    fprintf(file, "IP地址\t外网流量（字节）\n");
    for (int i = 0; i < stats->count; i++) {
        fprintf(file, "%s\t%lu\n", stats->ips[i], stats->traffic[i]);
    }

    fclose(file);
}